/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.lidovky.articles.domain;

/**
 *
 * @author Martin Řehánek (rehan@mzk.cz)
 */
public class Author {

    private boolean nameParsed = false;
    private String unresolvedName = null;
    private String[] firstNames = null;
    private String surname = null;
    private String nick = null;
    private boolean nameQuestioned = true;
    private int articleId;

    public Author(String unresolvedName, int articleId) {
        this.unresolvedName = unresolvedName;
        this.articleId = articleId;
        if (unresolvedName != null) {
            parseUnresolvedName();
        }
    }

    public boolean isUnknown() {
        return unresolvedName == null;
    }

    public boolean nameParsed() {
        return nameParsed;
    }

    public boolean hasFirstNames() {
        return firstNames.length != 0;
    }

    public boolean hasSurname() {
        return getSurname() != null;
    }

    public boolean hasNick() {
        return getNick() != null;
    }

    public boolean nameIsQuestioned() {
        return nameQuestioned;
    }

    /**
     * @return the unresolvedName
     */
    public String getUnresolvedName() {
        return unresolvedName;
    }

    /**
     * @return the firstNames
     */
    public String[] getFirstNames() {
        return firstNames;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @return the nick
     */
    public String getNick() {
        return nick;
    }

    private void parseUnresolvedName() {
        //names in format "MKR.[=Martin Karlovic Rehanek]" or "MKR.[=Rehanek,Martin Karlovic]" or "MKR.[=Martin Karlovic Rehanek?]" or "MKR.[=Rehanek,Martin Karlovic?]"
        if (unresolvedName.contains("[=") && unresolvedName.charAt(unresolvedName.length() - 1) == ']') {
            String withoutClosingBracket = unresolvedName.substring(0, unresolvedName.length() - 1);
            String[] nickAndName = withoutClosingBracket.split("\\[=");
            if (nickAndName.length == 2) {
                nick = nickAndName[0];
                String name = nickAndName[1];
                char lastChar = name.charAt(name.length() - 1);
                if (lastChar != '?') {//je jiste, ze nick patri jmenu
                    nameQuestioned = false;
                    parseNamesAndSurname(name);
                } else {
                    nameQuestioned = true;
                    parseNamesAndSurname(name.substring(0, name.length() - 1));
                }
            } else {
                surname = null;
                firstNames = new String[0];
            }
        } else {
            parseNamesAndSurname(unresolvedName);
        }
    }

    private void parseNamesAndSurname(String unresolvedName) {
        nameParsed = true;
        if (unresolvedName.contains(",")) {
            parseNamesAndSurnameCommaSeparated(unresolvedName);
        } else {
            parseNamesAndSurnameNatural(unresolvedName);
        }
    }

    //names in format "Martin Karlovic Rehanek"
    private void parseNamesAndSurnameNatural(String unresolvedName) {
        String[] strings = unresolvedName.split(" ");
        if (strings.length == 1) {
            //System.err.println("only single name in '" + unresolvedName + "', line: " + lineNumber);
            surname = strings[0];
            firstNames = new String[0];
        } else {
            firstNames = new String[strings.length - 1];
            for (int i = 0; i < strings.length; i++) {
                if (i == strings.length - 1) {
                    surname = strings[i];
                } else {
                    firstNames[i] = strings[i];
                }
            }
        }
    }

    //names in format "Rehanek,Martin Karlovic"
    private void parseNamesAndSurnameCommaSeparated(String unresolvedName) {
        String[] namesAndSurname = unresolvedName.split(",");
        if (namesAndSurname.length >= 1) {
            surname = namesAndSurname[0];
            firstNames = new String[0];
        }
        if (namesAndSurname.length == 2) {
            firstNames = namesAndSurname[1].split(" ");
        }
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append("unresolved name: ");
        result.append(unresolvedName);
        result.append('\n');
        result.append("articleId:");
        result.append(articleId);
        result.append('\n');
        result.append("first names: ");
        for (int i = 0; i < firstNames.length; i++) {
            result.append(firstNames[i]);
            result.append(' ');
        }
        result.append('\n');
        result.append("surname: ");
        result.append(surname);
        result.append('\n');
        result.append("nick: ");
        result.append(nick);
        result.append('\n');
        return result.toString();
    }
}
